﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace TS_XY_Train_Repaire.UserCon.Locomotive.MainBase.TrainDepSys
{
    public partial class uc_Locomotive_MainBase_Train_WorkPlace_ManaForm : DevExpress.XtraEditors.XtraForm
    {
        public string _LoadID, _LoadTCID, _LoadTDID, _LoadType = "";

        private void uc_Locomotive_MainBase_Train_WorkPlace_ManaForm_FormClosing(object sender, FormClosingEventArgs e)
        {
        }

        private void bt_Sure_Click(object sender, EventArgs e)
        {
            //try
            //{
                string _perfid = string.Empty;//专业
                foreach (Control c in flp_Perf.Controls)
                {
                    if (c is RadioButton)
                    {
                        if (((RadioButton)c).Checked)
                            _perfid = ((RadioButton)c).Tag.ToString();
                    }
                }
                string _typeid = string.Empty;//类型
                foreach (Control c in flp_WPT.Controls)
                {
                    if (c is RadioButton)
                    {
                        if (((RadioButton)c).Checked)
                            _typeid = ((RadioButton)c).Tag.ToString();
                    }
                }
                string _must = string.Empty;//机车必要要性编号
                foreach (Control c in flp_Must.Controls)
                {
                    if (c is RadioButton)
                    {
                        if (((RadioButton)c).Checked)
                            _must = ((RadioButton)c).Tag.ToString();
                    }
                }
                string _rank = string.Empty;//重要性编号
                foreach (Control c in flp_Rank.Controls)
                {
                    if (c is RadioButton)
                    {
                        if (((RadioButton)c).Checked)
                            _rank = ((RadioButton)c).Tag.ToString();
                    }
                }
                string _dtid = string.Empty;//作业类型
                foreach (Control c in flp_DataType.Controls)
                {
                    if (c is RadioButton)
                    {
                        if (((RadioButton)c).Checked)
                            _dtid = ((RadioButton)c).Tag.ToString();
                    }
                }
                string strPhoto = "0";
                if (cb_Photo.Checked)
                {
                    strPhoto = "1";
                }
                string sql = "";
                if (_LoadID != "")
                {
                    if (tb_Name.Text.Trim() != "" && tb_Brief.Text.Trim() != "" && tb_Rull.Text.Trim() != "")
                    {
                        if (_LoadType == "Add")
                        {

                            if (_LoadID.Substring(0, 1) == "D")
                            {
                                sql = "insert into TS_S_T_P_Train_WorkPlace(f_name,f_brief,f_tcid,f_tdid,f_typeid,f_order,f_rank,f_must,f_dtid,f_drid,f_drname,f_dataunit,f_data1,f_data2,f_photo,f_workerid,f_perfid) values ('" + tb_Name.Text.Trim() + "','" + tb_Brief.Text.Trim() + "'," + _LoadTCID + "," + _LoadTDID + "," + _typeid + "," + tb_Rull.Text.Trim() + "," + _rank + "," + _must + "," + _dtid + "," + cb_DRID_List.SelectedValue.ToString() + ",'" + cb_DRID_List.Text.Trim() + "','" + cb_DataUnit_List.Text.Trim() + "','" + tb_Data1.Text.Trim() + "','" + tb_Data2.Text.Trim() + "'," + strPhoto + "," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ","+_perfid+")";
                            }
                            else
                            {
                                sql = "insert into TS_S_T_P_Train_WorkPlace(f_name,f_brief,f_tcid,f_tdid,f_typeid,f_order,f_rank,f_must,f_dtid,f_drid,f_drname,f_dataunit,f_data1,f_data2,f_photo,f_workerid,f_perfid) values ('" + tb_Name.Text.Trim() + "','" + tb_Brief.Text.Trim() + "'," + _LoadTCID + "," + _LoadTDID + "," + _typeid + "," + tb_Rull.Text.Trim() + "," + _rank + "," + _must + "," + _dtid + "," + cb_DRID_List.SelectedValue.ToString() + ",'" + cb_DRID_List.Text.Trim() + "','" + cb_DataUnit_List.Text.Trim() + "','" + tb_Data1.Text.Trim() + "','" + tb_Data2.Text.Trim() + "'," + strPhoto + "," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + "," + _perfid + ")";
                            }
                        }
                        if (_LoadType == "Edit")
                        {
                            sql = "update TS_S_T_P_Train_WorkPlace set f_name='" + tb_Name.Text.Trim() + "',f_brief='" + tb_Brief.Text.Trim() + "',f_typeid=" + _typeid + ",f_order=" + tb_Rull.Text.Trim() + ",f_rank=" + _rank + ",f_must=" + _must + ",f_dtid=" + _dtid + ",f_drid=" + cb_DRID_List.SelectedValue.ToString() + ",f_drname='" + cb_DRID_List.Text.Trim() + "',f_dataunit='" + cb_DataUnit_List.Text.Trim() + "',f_data1='" + tb_Data1.Text.Trim() + "',f_data2='" + tb_Data2.Text.Trim() + "',f_Photo=" + strPhoto + ",f_perfid=" + _perfid + ",f_time=getdate(),f_workerid='" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + "' where f_id=" + _LoadID;
                        }
                        if (_LoadType == "Del")
                        {
                            sql = "update TS_S_T_P_Train_WorkPlace set f_del=1,f_time=getdate() where f_id=" + _LoadID;
                        }
                    }
                    else
                    {
                        MessageBox.Show("文本框值不能为空！");
                    }
                    TS_Win_Class.MySqlCom(sql);
                    if (!cb_ActType.Checked)
                    {
                        this.DialogResult = DialogResult.OK;
                    }
                }
                else
                {
                    MessageBox.Show("参数传递错误，请联系系统开发！");
                }
            //}
            //catch
            //{
            //    MessageBox.Show("数据错误！");
            //}
        }

        private void bt_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cb_Photo_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_Photo.Checked)
            {
                cb_Photo.Text = "必拍零件";
            }
            else
            {
                cb_Photo.Text = "选拍零件";
            }
        }

        private void uc_Locomotive_MainBase_Train_WorkPlace_ManaForm_Load(object sender, EventArgs e)
        {
            try
            {
                string strSQL = "";
                TS_Win_Class.Fill_FLP_RadioBox(flp_WPT, "select f_id,f_name from TS_S_T_P_WorkPlaceType where f_del=0");//项点类型下拉框
                TS_Win_Class.Fill_FLP_RadioBox(flp_Perf, "select f_id,f_name from TS_S_T_P_TrainPerf where f_del=0 and f_id in (select f_perfid from TS_S_T_P_TrainPerf_TC where f_del=0 and f_tcid="+_LoadTCID+")");//项点专业下拉框
                TS_Win_Class.Fill_FLP_RadioBox(flp_Must, "select f_id,f_name from TS_S_T_B_TrainMust where f_del=0");//机车重要性下拉框
                TS_Win_Class.Fill_FLP_RadioBox(flp_Rank, "select f_id,f_name from TS_S_T_B_TrainRank where f_del=0");//机车必要性下拉框
                TS_Win_Class.Fill_FLP_RadioBox(flp_DataType, "select f_id,f_name from TS_S_T_B_TrainDataType where f_del=0");//作业类型下拉框
                TS_Win_Class.FillListTypeAct(cb_DRID_List, "select f_id,f_name from TS_S_T_B_TrainDataResult where f_del=0");//机车数据逻辑
                TS_Win_Class.FillListTypeAct(cb_DataUnit_List, "select f_id,f_name from TS_S_T_B_TrainDataUnit where f_del=0");//机车数据单位

                if (_LoadID.Substring(0, 1) != "D")
                {
                    strSQL = "select * from TS_S_T_P_Train_WorkPlace where f_id=" + _LoadID;
                    SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
                    conn.Open();
                    SqlCommand comm = new SqlCommand(strSQL, conn);
                    SqlDataReader read = comm.ExecuteReader();
                    while (read.Read())
                    {
                        tb_Name.Text = read["f_name"].ToString().Trim();
                        tb_Brief.Text = read["f_brief"].ToString().Trim();
                        tb_Rull.Text = read["f_order"].ToString().Trim();
                        if ((Boolean)read["f_photo"])
                        {
                            cb_Photo.Checked = true;
                        }
                        else
                        {
                            cb_Photo.Checked = false;
                        }
                        tb_Data1.Text = read["f_data1"].ToString();
                        tb_Data2.Text = read["f_data2"].ToString();
                        string _perfid = read["f_perfid"].ToString();
                        foreach (Control c in flp_Perf.Controls)
                        {
                            if (c is RadioButton)
                            {
                                if (((RadioButton)c).Tag.ToString() == _perfid)
                                {
                                    ((RadioButton)c).Checked = true;
                                }

                            }
                        }
                        string _typeid = read["f_typeid"].ToString();
                        foreach (Control c in flp_WPT.Controls)
                        {
                            if (c is RadioButton)
                            {
                                if (((RadioButton)c).Tag.ToString() == _typeid)
                                {
                                    ((RadioButton)c).Checked = true;
                                }

                            }
                        }
                        string _rank = read["f_rank"].ToString();
                        foreach (Control c in flp_Rank.Controls)
                        {
                            if (c is RadioButton)
                            {
                                if (((RadioButton)c).Tag.ToString() == _rank)
                                {
                                    ((RadioButton)c).Checked = true;
                                }

                            }
                        }
                        string _must = read["f_must"].ToString();
                        foreach (Control c in flp_Must.Controls)
                        {
                            if (c is RadioButton)
                            {
                                if (((RadioButton)c).Tag.ToString() == _must)
                                {
                                    ((RadioButton)c).Checked = true;
                                }

                            }
                        }
                        string _dtid = read["f_dtid"].ToString();
                        foreach (Control c in flp_DataType.Controls)
                        {
                            if (c is RadioButton)
                            {
                                if (((RadioButton)c).Tag.ToString() == _dtid)
                                {
                                    ((RadioButton)c).Checked = true;
                                }

                            }
                        }
                        cb_DRID_List.Text = read["f_drname"].ToString();
                        cb_DataUnit_List.Text = read["f_dataunit"].ToString();
                        conn.Close();
                        conn.Dispose();
                    }
                }


                if (_LoadType == "Add")
                {
                    bt_Sure.Text = "确定新增项点";
                    bt_Sure.Enabled = true;

                }
                if (_LoadType == "Edit")
                {
                    bt_Sure.Text = "确定修改项点";
                    bt_Sure.Enabled = true;
                    cb_ActType.Visible = false;
                }
                if (_LoadType == "Del")
                {
                    bt_Sure.Text = "确定删除项点";
                    bt_Sure.Enabled = true;
                    cb_ActType.Visible = false;
                }
            }
            catch
            {

            }
        }

        public uc_Locomotive_MainBase_Train_WorkPlace_ManaForm()
        {
            InitializeComponent();
        }
    }
}